package com.ygy.yfzx.license.config;

import com.ygy.yfzx.license.LicenseVerify;
import com.ygy.yfzx.license.data.LicenseVerifyParam;
import com.ygy.yfzx.license.interceptor.LicenseCheckInterceptor;
import com.ygy.yfzx.license.listener.LicenseAppRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableConfigurationProperties(LicenseProperties.class)
@ConditionalOnProperty(prefix = "license",value = "enable",matchIfMissing = true)
public class LicenseAutoConfiguration {
   private LicenseProperties licenseProperties;

    @Autowired
    public void setLicenseProperties(LicenseProperties licenseProperties) {
        this.licenseProperties = licenseProperties;
    }


    @Bean
    @ConfigurationPropertiesBinding
    public LicenseCheckInterceptor licenseInterceptor(LicenseProperties customProperties) {
        return new LicenseCheckInterceptor(customProperties);
    }

    @Bean
    public WebMvcConfigurer licenseInterceptorConfigurer(LicenseCheckInterceptor licenseInterceptor) {
        System.out.println("LicenseAutoConfiguration！ WebMvcConfigurer"+licenseInterceptor +"licenseProperties:"+licenseProperties);
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(licenseInterceptor);
            }
        };
    }

    @Bean
    public ApplicationRunner licenseAppRunner() {
        System.out.println("LicenseAutoConfiguration！ licenseAppRunner"+licenseProperties);
        return new LicenseAppRunner(licenseProperties);
    }

}
